// xiosbase internal header (from <ios>)
#pragma once
#ifndef _XIOSBASE_
#define _XIOSBASE_
#ifndef RC_INVOKED
#include <xlocale>
#include <share.h>

#ifdef _MSC_VER
 #pragma pack(push,_CRT_PACKING)
 #pragma warning(push,3)
 #pragma warning(disable:4412)
#endif  /* _MSC_VER */

_STD_BEGIN

 #define _IOSskipws		0x0001
 #define _IOSunitbuf	0x0002
 #define _IOSuppercase	0x0004
 #define _IOSshowbase	0x0008
 #define _IOSshowpoint	0x0010
 #define _IOSshowpos	0x0020
 #define _IOSleft		0x0040
 #define _IOSright		0x0080
 #define _IOSinternal	0x0100
 #define _IOSdec		0x0200
 #define _IOSoct		0x0400
 #define _IOShex		0x0800
 #define _IOSscientific	0x1000
 #define _IOSfixed		0x2000

 #define _IOShexfloat	0x3000	// added with TR1/

 #define _IOSboolalpha	0x4000
 #define _IOS_Stdio		0x8000

 #define _IOS_Nocreate	0x40
 #define _IOS_Noreplace	0x80
 #define _IOSbinary		0x20

		// TEMPLATE CLASS _Iosb
template<class _Dummy>
	class _Iosb
	{	// define templatized bitmask/enumerated types, instantiate on demand
public:
	enum _Dummy_enum {_Dummy_enum_val = 1};	// don't ask

	enum _Fmtflags
		{	// constants for formatting options
		_Fmtmask = 0xffff, _Fmtzero = 0};

	static const _Fmtflags skipws = (_Fmtflags)_IOSskipws;
	static const _Fmtflags unitbuf = (_Fmtflags)_IOSunitbuf;
	static const _Fmtflags uppercase = (_Fmtflags)_IOSuppercase;
	static const _Fmtflags showbase = (_Fmtflags)_IOSshowbase;
	static const _Fmtflags showpoint = (_Fmtflags)_IOSshowpoint;
	static const _Fmtflags showpos = (_Fmtflags)_IOSshowpos;
	static const _Fmtflags left = (_Fmtflags)_IOSleft;
	static const _Fmtflags right = (_Fmtflags)_IOSright;
	static const _Fmtflags internal = (_Fmtflags)_IOSinternal;
	static const _Fmtflags dec = (_Fmtflags)_IOSdec;
	static const _Fmtflags oct = (_Fmtflags)_IOSoct;
	static const _Fmtflags hex = (_Fmtflags)_IOShex;
	static const _Fmtflags scientific = (_Fmtflags)_IOSscientific;
	static const _Fmtflags fixed = (_Fmtflags)_IOSfixed;

	static const _Fmtflags hexfloat =
		(_Fmtflags)_IOShexfloat;	// added with TR1/

	static const _Fmtflags boolalpha = (_Fmtflags)_IOSboolalpha;
	static const _Fmtflags _Stdio = (_Fmtflags)_IOS_Stdio;
	static const _Fmtflags adjustfield = (_Fmtflags)(_IOSleft
		| _IOSright | _IOSinternal);
	static const _Fmtflags basefield = (_Fmtflags)(_IOSdec
		| _IOSoct | _IOShex);
	static const _Fmtflags floatfield = (_Fmtflags)(_IOSscientific
		| _IOSfixed);

	enum _Iostate
		{	// constants for stream states
		_Statmask = 0x17};

	static const _Iostate goodbit = (_Iostate)0x0;
	static const _Iostate eofbit = (_Iostate)0x1;
	static const _Iostate failbit = (_Iostate)0x2;
	static const _Iostate badbit = (_Iostate)0x4;
	static const _Iostate _Hardfail = (_Iostate)0x10;

	enum _Openmode
		{	// constants for file opening options
		_Openmask = 0xff};

	static const _Openmode in = (_Openmode)0x01;
	static const _Openmode out = (_Openmode)0x02;
	static const _Openmode ate = (_Openmode)0x04;
	static const _Openmode app = (_Openmode)0x08;
	static const _Openmode trunc = (_Openmode)0x10;
	static const _Openmode _Nocreate = (_Openmode)_IOS_Nocreate;
	static const _Openmode _Noreplace = (_Openmode)_IOS_Noreplace;
	static const _Openmode binary = (_Openmode)_IOSbinary;

	enum _Seekdir
		{	// constants for file positioning options
		_Seekmask = 0x3};
	static const _Seekdir beg = (_Seekdir)0;
	static const _Seekdir cur = (_Seekdir)1;
	static const _Seekdir end = (_Seekdir)2;

	enum
		{	// constant for default file opening protection
		_Openprot = _SH_DENYNO};
	};

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::skipws;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::unitbuf;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::uppercase;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::showbase;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::showpoint;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::showpos;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::left;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::right;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::internal;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::dec;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::oct;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::hex;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::scientific;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::fixed;

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags
		_Iosb<_Dummy>::hexfloat;	// added with TR1

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::boolalpha;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::_Stdio;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::adjustfield;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::basefield;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::floatfield;

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Iostate _Iosb<_Dummy>::goodbit;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Iostate _Iosb<_Dummy>::eofbit;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Iostate _Iosb<_Dummy>::failbit;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Iostate _Iosb<_Dummy>::badbit;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Iostate _Iosb<_Dummy>::_Hardfail;

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::in;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::out;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::ate;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::app;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::trunc;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::_Nocreate;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::_Noreplace;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::binary;

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Seekdir _Iosb<_Dummy>::beg;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Seekdir _Iosb<_Dummy>::cur;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Seekdir _Iosb<_Dummy>::end;

		// CLASS ios_base
class _CRTIMP2_PURE ios_base
	: public _Iosb<int>
	{	// base class for ios
public:
	_BITMASK(_Fmtflags, fmtflags);
	_BITMASK(_Iostate, iostate);
	_BITMASK(_Openmode, openmode);
	_BITMASK(_Seekdir, seekdir);

	typedef std::streamoff streamoff;
	typedef std::streampos streampos;

	enum event
		{	// constants for ios events
		erase_event, imbue_event, copyfmt_event};

	typedef void (__CLRCALL_OR_CDECL *event_callback)(event, ios_base&, int);
	typedef unsigned int io_state, open_mode, seek_dir;

			// CLASS failure
	class failure
		: public runtime_error
		{	// base of all iostreams exceptions
	public:
		explicit __CLR_OR_THIS_CALL failure(const string &_Message)
			: runtime_error(_Message)
			{	// construct with message
			}

		virtual __CLR_OR_THIS_CALL ~failure() _THROW0()
			{	// destroy the object
			}

#if !_HAS_EXCEPTIONS

	protected:
		virtual void __CLR_OR_THIS_CALL _Doraise() const
			{	// report the exception
			_RAISE(*this);
			}
#endif  /* _HAS_EXCEPTIONS */

		};

			// CLASS Init
	class _CRTIMP2_PURE Init
		{	// controller for standard-stream initialization
	public:
		__CLR_OR_THIS_CALL Init()
            {
            _Init_ctor(this);
            }

		__CLR_OR_THIS_CALL ~Init()
            {
            _Init_dtor(this);
            }

	private:
        static _MRTIMP2_NPURE void __cdecl _Init_ctor(Init *);
        static _MRTIMP2_NPURE void __cdecl _Init_dtor(Init *);

		_MRTIMP2_NPURE static int& __cdecl _Init_cnt_func();	// net constructions - destructions
#ifdef _M_CEE_PURE
		static int& _Init_cnt;	// net constructions - destructions
#else
		__PURE_APPDOMAIN_GLOBAL static int _Init_cnt;	// net constructions - destructions
#endif
		};

	ios_base& __CLR_OR_THIS_CALL operator=(const ios_base& _Right)
		{	// assign state and format stuff from _Right
		if (this != &_Right)
			{	// worth doing
			_Mystate = _Right._Mystate;
			copyfmt(_Right);
			}
		return (*this);
		}

	__CLR_OR_THIS_CALL operator void *() const
		{	// test if any stream operation has failed
		return (fail() ? 0 : (void *)this);
		}

	bool __CLR_OR_THIS_CALL operator!() const
		{	// test if no stream operation has failed
		return (fail());
		}

    void __CLR_OR_THIS_CALL clear(iostate _State, bool _Reraise)
        {	// set state, possibly reraise exception
        _Mystate = (iostate)(_State & _Statmask);
        if ((_Mystate & _Except) == 0)
            ;
        else if (_Reraise)
            _RERAISE;
        else if (_Mystate & _Except & badbit)
            _THROW_NCEE(failure, "ios_base::badbit set");
        else if (_Mystate & _Except & failbit)
            _THROW_NCEE(failure, "ios_base::failbit set");
        else
            _THROW_NCEE(failure, "ios_base::eofbit set");
        }

	void __CLR_OR_THIS_CALL clear(iostate _State = goodbit)
		{	// set state to argument
		clear(_State, false);
		}

	void __CLR_OR_THIS_CALL clear(io_state _State)
		{	// set state to argument, old-style
		clear((iostate)_State);
		}

	iostate __CLR_OR_THIS_CALL rdstate() const
		{	// return stream state
		return (_Mystate);
		}


	void __CLR_OR_THIS_CALL setstate(iostate _State, bool _Exreraise)
		{	// merge in state argument, possibly reraise exception
		if (_State != goodbit)
			clear((iostate)((int)rdstate() | (int)_State), _Exreraise);
		}

	void __CLR_OR_THIS_CALL setstate(iostate _State)
		{	// merge in state argument
		if (_State != goodbit)
			clear((iostate)((int)rdstate() | (int)_State), false);
		}

	void __CLR_OR_THIS_CALL setstate(io_state _State)
		{	// merge in state argument, old style
		setstate((iostate)_State);
		}

	bool __CLR_OR_THIS_CALL good() const
		{	// test if no state bits are set
		return (rdstate() == goodbit);
		}

	bool __CLR_OR_THIS_CALL eof() const
		{	// test if eofbit is set in stream state
		return ((int)rdstate() & (int)eofbit);
		}

	bool __CLR_OR_THIS_CALL fail() const
		{	// test if badbit or failbit is set in stream state
		return (((int)rdstate()
			& ((int)badbit | (int)failbit)) != 0);
		}

	bool __CLR_OR_THIS_CALL bad() const
		{	// test if badbit is set in stream state
		return (((int)rdstate() & (int)badbit) != 0);
		}

	// This method is not used in msvcm80.dll, so we do not need to compile it.
	// Also, if we never change the exception mask, we ensure the clear() method
	// will never throw any exception. See VSW#476338 for details.
	iostate __CLR_OR_THIS_CALL exceptions() const
		{	// return exception mask
		return (_Except);
		}

	void __CLR_OR_THIS_CALL exceptions(iostate _Newexcept)
		{	// set exception mask to argument
		_Except = (iostate)((int)_Newexcept & (int)_Statmask);
		clear(_Mystate);
		}

	void __CLR_OR_THIS_CALL exceptions(io_state _State)
		{	// set exception mask to argument, old style
		exceptions((iostate)_State);
		}

	fmtflags __CLR_OR_THIS_CALL flags() const
		{	// return format flags
		return (_Fmtfl);
		}

	fmtflags __CLR_OR_THIS_CALL flags(fmtflags _Newfmtflags)
		{	// set format flags to argument
		fmtflags _Oldfmtflags = _Fmtfl;
		_Fmtfl = (fmtflags)((int)_Newfmtflags & (int)_Fmtmask);
		return (_Oldfmtflags);
		}

	fmtflags __CLR_OR_THIS_CALL setf(fmtflags _Newfmtflags)
		{	// merge in format flags argument
		ios_base::fmtflags _Oldfmtflags = _Fmtfl;
		_Fmtfl = (fmtflags)((int)_Fmtfl
			| (int)_Newfmtflags & (int)_Fmtmask);
		return (_Oldfmtflags);
		}

	fmtflags __CLR_OR_THIS_CALL setf(fmtflags _Newfmtflags, fmtflags _Mask)
		{	// merge in format flags argument under mask argument
		ios_base::fmtflags _Oldfmtflags = _Fmtfl;
		_Fmtfl = (fmtflags)(((int)_Fmtfl & (int)~_Mask)
			| ((int)_Newfmtflags & (int)_Mask & (int)_Fmtmask));
		return (_Oldfmtflags);
		}

	void __CLR_OR_THIS_CALL unsetf(fmtflags _Mask)
		{	// clear format flags under mask argument
		_Fmtfl = (fmtflags)((int)_Fmtfl & (int)~_Mask);
		}

	streamsize __CLR_OR_THIS_CALL precision() const
		{	// return precision
		return (_Prec);
		}

	streamsize __CLR_OR_THIS_CALL precision(streamsize _Newprecision)
		{	// set precision to argument
		streamsize _Oldprecision = _Prec;
		_Prec = _Newprecision;
		return (_Oldprecision);
		}

	streamsize __CLR_OR_THIS_CALL width() const
		{	// return width
		return (_Wide);
		}

	streamsize __CLR_OR_THIS_CALL width(streamsize _Newwidth)
		{	// set width to argument
		streamsize _Oldwidth = _Wide;
		_Wide = _Newwidth;
		return (_Oldwidth);
		}

	locale __CLR_OR_THIS_CALL getloc() const
		{	// get locale
		return (*_Ploc);
		}

	locale __CLR_OR_THIS_CALL imbue(const locale& _Loc)
        {	// set locale to argument
        locale _Oldlocale = *_Ploc;
        *_Ploc = _Loc;
        _Callfns(imbue_event);
        return (_Oldlocale);
        }

	static int __CLRCALL_OR_CDECL xalloc()
		{	// allocate new iword/pword index
		_BEGIN_LOCK(_LOCK_STREAM)	// lock thread to ensure atomicity
			return (_Index++);
		_END_LOCK()
		}

	long& __CLR_OR_THIS_CALL iword(int _Idx)
		{	// return reference to long element
		return (_Findarr(_Idx)._Lo);
		}

	void *& __CLR_OR_THIS_CALL pword(int _Idx)
		{	// return reference to pointer element
		return (_Findarr(_Idx)._Vp);
		}

	void __CLR_OR_THIS_CALL register_callback(event_callback _Pfn, int _Idx)
        {	// register event handler
        _Calls = _NEW_CRT _Fnarray(_Idx, _Pfn, _Calls);
        }

	ios_base& __CLR_OR_THIS_CALL copyfmt(const ios_base& _Other)
        {	// copy format stuff
        if (this != &_Other)
            {	// copy all but _Mystate
            _Tidy();
            *_Ploc = *_Other._Ploc;
            _Fmtfl = _Other._Fmtfl;
            _Prec = _Other._Prec;
            _Wide = _Other._Wide;
            _Iosarray *_Ptr = _Other._Arr;

            for (_Arr = 0; _Ptr != 0; _Ptr = _Ptr->_Next)
                if (_Ptr->_Lo != 0 || _Ptr->_Vp != 0)
                    {	// copy over nonzero array values
                    iword(_Ptr->_Index) = _Ptr->_Lo;
                    pword(_Ptr->_Index) = _Ptr->_Vp;
                    }

            for (_Fnarray *_Q = _Other._Calls; _Q != 0; _Q = _Q->_Next)
                register_callback(_Q->_Pfn, _Q->_Index);	// copy callbacks

            _Callfns(copyfmt_event);	// call callbacks
            exceptions(_Other._Except);	// cause any throw at end
            }
        return (*this);
        }


	virtual __CLR_OR_THIS_CALL ~ios_base()
        {
        _Ios_base_dtor(this);
        }

	static bool __CLRCALL_OR_CDECL sync_with_stdio(bool _Newsync = true)
		{	// set C/C++ synchronization flag from argument
		_BEGIN_LOCK(_LOCK_STREAM)	// lock thread to ensure atomicity
			const bool _Oldsync = _Sync;
			_Sync = _Newsync;
			return (_Oldsync);
		_END_LOCK()
		}

	static _MRTIMP2_NPURE void __cdecl _Addstd(ios_base *);
	size_t _Stdstr;	// if > 0 index of standard stream to suppress destruction

protected:
	__CLR_OR_THIS_CALL ios_base()
		{	// default constructor
		}

	void __CLR_OR_THIS_CALL _Init()
        {	// initialize a new ios_base
        _Ploc = 0;
        _Stdstr = 0;
        _Except = goodbit;
        _Fmtfl = skipws | dec;
        _Prec = 6;
        _Wide = 0;
        _Arr = 0;
        _Calls = 0;
        clear(goodbit);
        _Ploc = _NEW_CRT locale;
        }

private:
			// STRUCT _Iosarray
	struct _Iosarray
		{	// list element for open-ended sparse array of longs/pointers
	public:
		__CLR_OR_THIS_CALL _Iosarray(int _Idx, _Iosarray *_Link)
			: _Next(_Link), _Index(_Idx), _Lo(0), _Vp(0)
			{	// construct node for index _Idx and link it in
			}

		_Iosarray *_Next;	// pointer to next node
		int _Index;	// index of this node
		long _Lo;	// stored long value
		void *_Vp;	// stored pointer value
		};

			// STRUCT _Fnarray
	struct _Fnarray
		{	// list element for open-ended sparse array of event handlers
		__CLR_OR_THIS_CALL _Fnarray(int _Idx, event_callback _Pnew, _Fnarray *_Link)
			: _Next(_Link), _Index(_Idx), _Pfn(_Pnew)
			{	// construct node for index _Idx and link it in
			}

		_Fnarray *_Next;	// pointer to next node
		int _Index;	// index of this node
		event_callback _Pfn;	// pointer to event handler
		};

	void __CLR_OR_THIS_CALL _Callfns(event _Ev)
        {	// call all event handlers, reporting event
        for (_Fnarray *_Ptr = _Calls; _Ptr != 0; _Ptr = _Ptr->_Next)
            (*_Ptr->_Pfn)(_Ev, *this, _Ptr->_Index);
        }

	_Iosarray& __CLR_OR_THIS_CALL _Findarr(int _Idx)
        {	// locate or make a variable array element
        _Iosarray *_Ptr, *_Q;

        for (_Ptr = _Arr, _Q = 0; _Ptr != 0; _Ptr = _Ptr->_Next)
            if (_Ptr->_Index == _Idx)
                return (*_Ptr);	// found element, return it
            else if (_Q == 0 && _Ptr->_Lo == 0 && _Ptr->_Vp == 0)
                _Q = _Ptr;	// found recycling candidate

        if (_Q != 0)
            {	// recycle existing element
            _Q->_Index = _Idx;
            return (*_Q);
            }

        _Arr = _NEW_CRT _Iosarray(_Idx, _Arr);	// make a new element
        return (*_Arr);
        }

	void __CLRCALL_OR_CDECL _Tidy()
        {	// discard storage for an ios_base
        _Callfns(erase_event);
        _Iosarray *_Q1, *_Q2;

        for (_Q1 = _Arr; _Q1 != 0; _Q1 = _Q2)
            _Q2 = _Q1->_Next, _DELETE_CRT(_Q1);	// delete array elements
        _Arr = 0;

        _Fnarray *_Q3, *_Q4;
        for (_Q3 = _Calls; _Q3 != 0; _Q3 = _Q4)
            _Q4 = _Q3->_Next, _DELETE_CRT(_Q3);	// delete callback elements
        _Calls = 0;
        }

    static _MRTIMP2_NPURE void __cdecl _Ios_base_dtor(ios_base *);

	iostate _Mystate;	// stream state
	iostate _Except;	// exception mask
	fmtflags _Fmtfl;	// format flags
	streamsize _Prec;	// field precision
	streamsize _Wide;	// field width
	_Iosarray *_Arr;	// pointer to first node of long/pointer array
	_Fnarray *_Calls;	// pointer to first node of call list
	locale *_Ploc;	// pointer to locale

#if defined(_M_CEE_PURE)
	static int& _Index;	// source of unique indexes for long/pointer array
	static bool& _Sync;	// C/C++ synchronization flag (ignored)
#else
	__PURE_APPDOMAIN_GLOBAL static int _Index;	// source of unique indexes for long/pointer array
	__PURE_APPDOMAIN_GLOBAL static bool _Sync;	// C/C++ synchronization flag (ignored)
#endif
    static _MRTIMP2_NPURE int& __cdecl _Index_func();
    static _MRTIMP2_NPURE bool& __cdecl _Sync_func();
	};

_BITMASK_OPS(ios_base::_Fmtflags)
_BITMASK_OPS(ios_base::_Iostate)
_BITMASK_OPS(ios_base::_Openmode)
_BITMASK_OPS(ios_base::_Seekdir)
_STD_END
#ifdef _MSC_VER
 #pragma warning(pop)
 #pragma pack(pop)
#endif  /* _MSC_VER */

#endif /* RC_INVOKED */
#endif  /* _XIOSBASE_ */

/*
 * Copyright (c) 1992-2006 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 V5.02:0009 */
